//点淘版本 2.26.19
//update 2021.11.6
//增加招财鸭 精简代码
//update 2021.11.7
//修复bug
//2021.11.12
//删除抽奖、招财鸭活动


"ui";



var entries = "走路赚钱|领步数"
var model = device.model
var num = 30
if(model == "MEIZU M6"){
    num = 15
    bushu = 12500
} else {
    num = 50
    bushu = 17550
}
ui.layout(
    <vertical>
            <appbar>
                <toolbar id = "toolbar" title = "点淘任务" bg = "#FF00FF"></toolbar>
            </appbar>
            <card w="*" margin="10" paddingleft = "15">
                <horizontal>
                    <text textSize="16sp">选择任务</text>
                    <spinner id = "choose" entries = "{{entries}}"></spinner>
                </horizontal>
            </card>
            <card w="*" margin="10" paddingleft = "15">
                <vertical>
                    <horizontal>
                        <text textSize="16sp">走路赚钱视频个数：</text>
                        <input id = "spnum" w="50" text="{{num}}"/>
                    </horizontal>
                    <horizontal>
                        <text textSize="16sp">抽奖看视频的视频个数：</text>
                        <input id = "cjspnum" w="50" text="{{num}}"/>
                    </horizontal>
                    <horizontal>
                        <text textSize="16sp">设置步数：</text>
                        <input id = "bushu" w="70" text="{{bushu}}"/>
                    </horizontal>
                    <text textSize="18sp" color="red">提示：</text>
                    <text textSize="18sp" paddingleft = "15">【走路赚钱】结束后 -->【领步数】</text>                                                           
                </vertical>
            </card>
            <card w="*" margin="10" paddingleft = "15" >
                <button id = "start" text="启动"/>
            </card>
    </vertical>

    
);
var spnum = 30;
var cjspnum = 30;
var task = 0;
ui.start.on("click", ()=>{
    var i = ui.choose.getSelectedItem();
    task = ui.choose.getSelectedItemPosition()
    toast("开始-> " + i);
    spnum = parseInt(ui.spnum.getText())
    cjspnum = parseInt(ui.cjspnum.getText())
    bushu = parseInt(ui.bushu.getText())
    threads.start(function(){
        xuanfu()
    })
    main()
});

function main(){
    threads.start(function(){
        if(task == 0){
            打开应用()
            走路赚钱()
            关闭应用("点淘")
            sleep(2500)
            打开应用()
            步数()
            sleep(2000)
            exit()
        }
        if(task == 1){
            打开应用()
            步数()
            exit()
        }
    })

}

function 循环看视频(str){
    while(true) {
        if(text("去观看").findOnce()) {
            sleep(random(1500,2500))
            点击不可点击文本("去观看")
            console.log("看视频赚步数")
            sleep(random(2000,4500))
            console.log("等待进入直播界面")
            id("com.taobao.live:id/task_center_action").waitFor()
            console.log("进入直播界面")
            sleep(3500)
            
            while(true) {
                sleep(random(2000,3500))
                if(!id("com.taobao.live:id/task_center_action").findOnce()) {
                    console.log("即将开始下一次任务")
                    sleep(random(2000,3500))
                    back()
                    sleep(1500)
                    if(text("残忍离开").findOnce()){
                        点击不可点击文本("残忍离开")
                        sleep(1500)
                        wait("赚步数")
                    }
                    console.log("等待回到走路赚钱界面")
                    wait(str)
                    console.log("回到走路赚钱界面")
                    sleep(900)
                    break
                }else {
                    sleep(random(2000,4500))
                    滑动看视频()
                    sleep(random(2000,4500))
                }
            }
        } else {
            toastLog("观看结束。。")
            break
        }
    }
}

function 步数() {
    稍向下滑动()
    if(text("走路赚元宝").findOnce()) {
        等待时间(5)
        点击不可点击文本("走路赚元宝")
        console.log("进入走路赚钱界面")
        wait("连续走")
        sleep(1500)
        if(text("出发").findOnce()) {
            sleep(1500)
            点击不可点击文本("出发")
            sleep(random(1500,2500))
        }
        sleep(1500)
        if(text("去领步数").findOnce()) {
            sleep(1500)
            点击不可点击文本("去领步数")
            sleep(random(1500,2500))
        }
    }
    while(true) {
        if(text("去领步数").findOnce()) {
            sleep(1500)
            点击不可点击文本("去领步数")
            sleep(random(1500,2500))
        }
            var 领取s = text("领取").find()
            if(领取s.size() > 1){
                var 领取 = 领取s.get(1).bounds()
            } else {
                if(领取s.size() == 0){
                    console.log("无可领取，即将结束。")
                    break
                } else{
                    var 领取 = 领取s.get(0).bounds()
                }
            }
            if(领取) {
                sleep(random(1500,2500))
                console.log("点击领取")
                click(领取.centerX()+random(2,5),领取.centerY()+random(2,5))
                sleep(random(2000,3500))
                if(text("我知道了").findOnce()){
                    点击不可点击文本("我知道了")
                    sleep(1500)
                }
                if(textContains("再得").findOnce()) {
                    textContains("再得").click()
                    console.log("等待进入直播界面")
                    sleep(random(2000,3500))
                    id("com.taobao.live:id/task_center_action").waitFor()
                    console.log("进入直播界面")
                    sleep(900)
                    循环看视频("规则")
                    sleep(random(2000,3500))
                }
            } else {
                console.log("结束")
                break
            } 
    }
}
function 打开应用() {
    等待时间(5)
    app.launchApp("点淘")   //打开应用
    toastLog("打开app 等待8～12s")
    sleep(random(8000,12000)) //打开应用等待8～12s
    wait("视频")
    console.log("进入app界面")
    sleep(1500)
    if(text("视频").findOnce()) {
        if(text("我的").findOnce()){
            点击不可点击文本("我的")
            sleep(900)
        } else {
            var w = device.width
            var h = device.height
            click(w/9,h/25*24) // 隐藏虚拟导航栏
            sleep(1500)
            click(w/9*8,h/25*24)

        }
    console.log("点击 我的")
    id("com.taobao.live:id/personal_menu").waitFor()
    等待时间(5)
    if(text("元宝中心").findOnce()) {
        点击不可点击文本("元宝中心")
        console.log("进入元宝中心")
        等待时间(8)
        wait("去参与")
    }
    等待时间(5)
    sleep(random(2000,4500))
    if(text("立即签到").findOnce()) {
        sleep(1500)
        点击不可点击文本("立即签到")
        console.log("签到")
        sleep(random(1500,3000))
        wait("20:00-24:00")
        sleep(500)
        back()
        console.log("签到成功，正在返回。。。")
        sleep(random(1500,3000))
    }
    sleep(random(2000,4500))
    }
    if(model != "MEIZU M6") {
        if(className("android.view.View").depth(10).drawingOrder(0).indexInParent(0).findOnce()){
            等待时间(5)
            className("android.view.View").depth(10).drawingOrder(0).indexInParent(0).click()
            console.log("关闭按钮")
            等待时间(5)
        }
    }
    孵化蛋()
    sleep(random(1500,3000))
    if(text("今日签到").findOnce()) {
        sleep(1500)
        点击不可点击文本("今日签到")
        wait("活动规则")
        sleep(1500)
        if(text("点击签到").findOnce()) {
            sleep(1500)
            点击不可点击文本("点击签到")
            console.log("签到成功")
        }
        console.log("签到")
        sleep(random(1500,3000))
        wait("20:00-24:00")
        sleep(500)
        back()
        console.log("签到成功，正在返回。。。")
        sleep(random(1500,3000))
    }
}


//孵化蛋 
function 孵化蛋() {
    if(text("TB1qYr6pmslXu8jSZFuXXXg7FXa-500-500").findOnce()) {
        等待时间(5)
        console.log("领取孵化蛋奖励")
        点击不可点击文本("TB1qYr6pmslXu8jSZFuXXXg7FXa-500-500")
        sleep(1500)
        if(textContains("去看直播").findOnce()) {
            textContains("去看直播").click()
            console.log("等待进入直播界面")
            sleep(random(2000,3500))
            id("com.taobao.live:id/task_center_action").waitFor()
            console.log("进入直播界面")
            sleep(900)
            while(true) {
                if(!id("com.taobao.live:id/task_center_action").findOnce()) {
                    console.log("即将开始下一次任务")
                    sleep(random(2000,3500))
                    back()
                    sleep(random(2000,3500))
                    if(text("残忍离开").findOnce()){
                        点击不可点击文本("残忍离开")
                        sleep(900)
                    }
                    sleep(2000)
                    console.log("等待回到元宝中心界面")
                    wait("去参与")
                    console.log("回到元宝中心界面")
                    sleep(900)
                    break
                }else {
                    sleep(random(2000,45000))
                    滑动看视频()
                }
            }
            sleep(random(2000,3500))
        } else {
            wait("O1CN01LxFPWH1Mmy2hurJW4_!!6000000001478-2-tps-54-54.png?getAvatar_")
            sleep(1500)
            点击不可点击文本("O1CN01LxFPWH1Mmy2hurJW4_!!6000000001478-2-tps-54-54.png?getAvatar_") //关闭按钮
        }
        sleep(1500)
    }
}



//走路赚钱
function 走路赚钱() {
    var quguankan = 0
    等待时间(5)
    稍向下滑动()
    wait("走路赚元宝")
    if(text("走路赚元宝").findOnce()) {
        等待时间(5)
        点击不可点击文本("走路赚元宝")
        console.log("进入走路赚钱界面")
        等待时间(5)
    while(true) {
        //赚步数
        wait("赚步数")
         //领取能量饮料
         sleep(random(2000,4500))
         if(text("领取").findOnce()){
             等待时间(5)
             点击不可点击文本("领取")
             等待时间(5)
         }

         //获取已经获得的步数，后期可能会有变化(魅族)
         if(className("android.view.View").depth(9).drawingOrder(0).indexInParent(1).find()) {
            var 步数s = className("android.view.View").depth(17).drawingOrder(0).indexInParent(1).find().size()
            var 步数 = className("android.view.View").depth(17).drawingOrder(0).indexInParent(1).find().get(步数s-1).getText()
            toastLog(步数)
        }   
        if(quguankan > spnum || 步数 > bushu ){
            toastLog("观看超过"+ spnum +"次或超过+"+bushu+"步，即将进行步数领取。。。")
            sleep(2000)
            break
        } else{
            if(text("赚步数").findOnce()) {
                sleep(random(2500,4500))
                点击不可点击文本("赚步数")
                sleep(random(1500,2500))
                wait("去邀请")
                sleep(900)
                if(text("去观看").findOnce()) {
                    sleep(random(1500,2500))
                    点击不可点击文本("去观看")
                    console.log("看视频赚步数")
                    quguankan = quguankan + 1
                    sleep(random(2000,4500))
                    console.log("等待进入直播界面")
                    id("com.taobao.live:id/task_center_action").waitFor()
                    console.log("进入直播界面")
                    sleep(3500)
                    
                    while(true) {
                        sleep(random(2000,3500))
                        if(!id("com.taobao.live:id/task_center_action").findOnce()) {
                            console.log("即将开始下一次任务")
                            sleep(random(2000,3500))
                            back()
                            sleep(1500)
                            if(text("残忍离开").findOnce()){
                                点击不可点击文本("残忍离开")
                                sleep(1500)
                                wait("赚步数")
                            }
                            console.log("等待回到走路赚钱界面")
                            wait("规则")
                            console.log("回到走路赚钱界面")
                            sleep(900)
                            break
                        }else {
                            sleep(random(2000,4500))
                            滑动看视频()
                            sleep(random(2000,4500))
                        }
                    }
                    sleep(random(1500,2500))
                    //领取能量饮料
                    if(text("领取").findOnce()){
                        sleep(random(1500,2500))
                        点击不可点击文本("领取")
                        sleep(random(1500,2500))
                        等待时间(5)
                    }
                }else {
                    稍向下滑动()
                    console.log("没找到观看任务")
                    等待时间(3)
                    去观看 = 去观看 + 1
                    console.log(去观看)
                    break
                }
            }
        }

        }
    }
}
function 关闭应用(str) {
    // sleep(5000)
    openAppSetting(getPackageName(str)); //关闭应用的名字
    sleep(5000)

    while(true) {
        if(text("强行停止").findOnce()) {
            text("强行停止").click()
            sleep(1500)
            if(text("确定").findOnce()){
                text("确定").click() //点击强行关闭后弹出的确认框
                sleep(1000)
                console.log("关闭应用")
                break
            }
            if(text("强行停止").findOnce()){
                text("强行停止").click()
                sleep(1000)
                console.log("关闭应用")
                break
            }
            
        }
    }
}



//悬浮窗
//=====================================================
function xuanfu() {
    var window = floaty.window(
        <frame padding="1">
            <vertical  >
                <horizontal>
                    <button id="action" text="点击" w="35" h="50" style="Widget.AppCompat.Button.Colored" textSize="12sp" />
                    <button id="begin" text="日志" w="35" h="50" textSize="12sp" />
                    <button id="end" text="结束" w="35" h="50" textSize="12sp" />
                    <button id="times" text="计时" w="35" h="50" textSize="12sp" />
                </horizontal>

                <com.stardust.autojs.core.console.ConsoleView h="150" w="180" id="console" bg="#80000000" />


            </vertical>
        </frame>

    );

    window.console.setConsole(runtime.console);
    window.setPosition(0, device.height / 6 * 5);
    // window.console.findViewById(org.autojs.autojs.R.id.input_container).setVisibility(android.view.View.GONE);
    window.console.attr("visibility", "gone");
    
    //开始计时
    var before = new Date();

    //日志和结束按钮不显示
    window.begin.setVisibility(8)
    window.end.setVisibility(8)
    window.times.setVisibility(8)
    // window.console.setTouchable(false);

    var x = 0, y = 0;
    var windowX, windowY;
    var downTime;
    window.action.setOnTouchListener(function (view, event) {
        switch (event.getAction()) {
            case event.ACTION_DOWN:
                x = event.getRawX();
                y = event.getRawY();
                windowX = window.getX();
                windowY = window.getY();
                downTime = new Date().getTime();
                return true;
            case event.ACTION_MOVE:
                //移动手指时调整悬浮窗位置
                window.setPosition(windowX + (event.getRawX() - x),
                    windowY + (event.getRawY() - y));
                return true;
            case event.ACTION_UP:
                //手指弹起时如果偏移很小则判断为点击
                if (Math.abs(event.getRawY() - y) < 5 && Math.abs(event.getRawX() - x) < 5) {
                    onClick();
                }
                return true;
        }
        return true;
    });

    function onClick(str, sec) {

        if (window.end.visibility == 8) {
            window.console.setVisibility(0)
            window.end.setVisibility(0)
            window.times.setVisibility(0)
        } else {
            window.console.setVisibility(8)
            window.end.setVisibility(8)
            window.times.setVisibility(8)
        }
    }

    var timerId;

    function 显示状态信息(str, sec) {
        try {
            clearTimeout(timerId);
        }
        catch (error) {
            toastLog("error");
        }
        ui.run(() => {
            window.action.setText(str);
        });
        timerId = setTimeout(function () {
            ui.run(() => {
                window.action.setText("");
            });
        }, sec * 1000);
    }


    window.begin.click(() => {
        window.begin.text() == "显示日志"
        if (window.begin.text() == "显示日志") {
            window.begin.setText("隐藏日志")
            window.console.attr("visibility", "visible");
        } else {
            window.begin.setText("显示日志")
            window.console.attr("visibility", "gone");
        }
    });

    window.end.click(() => {
        floaty.closeAll();
        engines.stopAll();
        exit();
    });
    window.times.click(() => {
        var now =new Date();
        var last = Math.floor((now - before) / 1000)
        if(last >= 60) {
            var minutes =  Math.floor(last / 60)
        } else {
            var minutes = 0
        }
        var seconds = last - minutes * 60
        if(minutes > 0 && seconds < 10){
            console.log("运行了：" + minutes + "分 " + "0" + seconds + "秒")
        } 
        if(minutes > 0 && seconds > 10){
            console.log("运行了：" + minutes + "分 "  + seconds + "秒")
        }
        if(minutes <= 0 && seconds < 10){
            console.log("运行了：" + "0" + seconds + "秒")
        } 
        if(minutes <= 0 && seconds > 10){
            console.log("运行了："  + seconds + "秒")
        } 
    });

}
//悬浮窗结束-----------
//稍向下滑动
function 稍向下滑动() {
    sleep(2000)
    var width = device.width
    var height = device.height
    var suiji = parseInt(Math.random() * 160)
    var end = parseInt(Math.random() * 560)
    var time = random(2000,4500)
    swipe(width / 2, height - 100, width / 2 - suiji, height - 600, time)
}
//稍向上滑动
function 稍向上滑动() {
    sleep(2000)
    var width = device.width
    var height = device.height
    var suiji = parseInt(Math.random() * 160)
    var end = parseInt(Math.random() * 560)
    var time = random(2000,4500)
    swipe(width / 2,  height - 600, width / 2 - suiji,height - 100, time)
}
//滑动
function 向下滑动() {
    sleep(2000)
    var width = device.width
    var height = device.height
    var suiji = parseInt(Math.random() * 190)
    var end = parseInt(Math.random() * 560)
    swipe(width / 2, height - 50 + random(20,50), width / 2 - suiji, end, 7000)
    sleep(3000)
}

//滑动看视频
function 滑动看视频() {
    sleep(2000)
    var width = device.width
    var height = device.height
    var suiji = parseInt(Math.random() * 160)
    var end = parseInt(random(50,100))
    var times = random(1500,2500)
    swipe(width / 2, height / 2 + 150 + random(50,80), width / 2 - suiji, end, times)
    sleep(3000)
    console.log("看视频")
}



//等待时间
function 等待时间(number) {
    var maxtime = parseInt(Math.random() * number )
    var lasttime = maxtime * 1000
    sleep(lasttime)
}

//等待出现模块
function wait(Name) {
    console.log("等待->",Name)
    text(Name).waitFor()
    console.log("找到->",Name)    
    sleep(1000)
}

//点击文本
function 点击文本(str) {
    if (text(str).findOnce()) {
        text(str).click()
        sleep(500)
    }
}

//点击不可点击id
function 点击不可点击id(str) {
    var strname = id(str).findOnce().bounds()
    click(strname.centerX()+random(2,5), strname.centerY()+random(2,5))
}
//点击不可点击文本
function 点击不可点击文本(str) {
    var strname = text(str).findOnce().bounds()
    click(strname.centerX()+random(2,5), strname.centerY()+random(2,5))
}

function 通知(title,content) {
    var storage = storages.create("main")
    let token = storage.get("token");
    var model = storage.get("model")
    let url = "http://www.pushplus.plus/send";
    let r = http.postJson(url, {
        token: token,
        title: title,
        content: "账号：" + model + content,
    })
    toastLog(r.body.json().msg);
}

function getMemoryUsage(){
    var usage = (100 * device.getAvailMem() / device.getTotalMem());
    //保留一位小数
    return Math.round(usage * 100) / 100;
}